<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/db.php');
class film_rating
{
	private $db;
	private $table_name = 'film_rating';
	public static $film_rating = array(
		'CN'=>array(
			'forbidden' => 1,
			'allowed' => 2,
			'inattention' => 3,
		),
		'HK'=>array(
			'未知' => 0,
			'Ⅰ' => 2,
			'ⅡA' => 3,
			'ⅡB' => 4,
			'Ⅲ' => 5,
		),
		'TW'=>array(
			'未知' => 0,
			'普' => 2,
			'護' => 3,
			'輔' => 4,
			'限' => 5,
		),
		'US'=>array(
			'未知' => 0,
			'G' => 2,
			'PG' => 3,
			'PG-13' => 4,
			'R' => 5,
			'NC-17' => 6,
			'UR' => 7,
			'R' => 8,
		),
	);
	function __construct()
	{
		$this->db = new db();
	}
	public function insert($data)
	{
		if(!isset($data['film_id'])||empty($data['film_id']))
		{
			return false;
		}
		$data_for_query['film_id'] = intval($data['film_id']);
		if(isset($data['CN'])&&!empty($data['CN']))
		{
			$data_for_query['CN'] = intval($data['CN']);
		}
		if(isset($data['HK'])&&!empty($data['HK']))
		{
			$data_for_query['HK'] = intval($data['HK']);
		}
		if(isset($data['TW'])&&!empty($data['TW']))
		{
			$data_for_query['TW'] = intval($data['TW']);
		}
		if(isset($data['US'])&&!empty($data['US']))
		{
			$data_for_query['US'] = intval($data['US']);
		}
		$sql = "INSERT INTO `".$this->table_name."` (".$this->db->implodeToColumn(array_keys($data_for_query)).") VALUES (".$this->db->implodeToValues(array_values($data_for_query)).")";
		$count = $this->db->query($sql);
		if(empty($count))
		{
			//throw new Exception("INSERT INTO `".$this->table_name."` faild");
			throw new Exception("");
		}
		else
		{
			return $count;
		}
	}
	
	public function getRatingByFilmId($film_id)
	{
		$sql_1 = "SELECT * FROM `".$this->table_name."` WHERE `film_id`=".$film_id;
		$result_1 = $this->db->query($sql_1);
		$result_1->setFetchMode(PDO::FETCH_ASSOC);
		$fetch_result = $result_1->fetchAll();
		$rating = $fetch_result[0];
		if(empty($rating))
		{
			//throw new Exception("faild");
			throw new Exception("");
		}
		else
		{
			return $rating;
		}
	}
	
	public function update($where,$data)
	{
		if(isset($data['CN'])&&!empty($data['CN']))
		{
			$data_for_query['CN'] = intval($data['CN']);
		}
		if(isset($data['HK'])&&!empty($data['HK']))
		{
			$data_for_query['HK'] = intval($data['HK']);
		}
		if(isset($data['TW'])&&!empty($data['TW']))
		{
			$data_for_query['TW'] = intval($data['TW']);
		}
		if(isset($data['US'])&&!empty($data['US']))
		{
			$data_for_query['US'] = intval($data['US']);
		}
		if(!empty($data_for_query))
		{
			$sql = "UPDATE `".$this->table_name."` SET ".$this->db->implodeToUpdate($data_for_query)." WHERE ".$this->db->implodeToWhere($where);
			$count = $this->db->query($sql);
			return $count;
		}
		return true;
	}
	
	public function updateByFilmId($film_id,$data)
	{
		$where = array(
			'film_id' => $film_id,
		);
		return $this->update($where,$data);
	}
}

?>
